/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

window.addEvent("domready", function()
{
  // Display predefined sector
  var sector = new Sector("PI 4-669", "PI 4-669", Global.CachedMaps["PI 4-669"]);
  Global.DisplayedSector = sector;
  Panels.Main.Render();

  // Add an event handler to SavedSectors
  $("SavedSectors").addEvent("change", function()
  {
    $("SectorName").set("value", this.getSelected().get("value"));
  });
});

Panels.Main.DisplaySaved = function(result)
{
  var sectors = result["Sectors"];
  var select = $("SavedSectors");
  sectors.sort();

  for (var i = 0; i < sectors.length; i++)
  {
    var sector = sectors[i];
    Global.Sectors[sector] = new Sector(sector);

    new Element("option", { value: sector, html: sector })
      .addEvent("dblclick", function(){ Panels.Main.Load() })
      .inject(select, "bottom");
  }
}

EventSink.addEvent(MsgLogin, Panels.Main.DisplaySaved);

Panels.Main.New = function()
{
  SelectSectorWindow.Show(function(sector)
  {
    var options = $("SavedSectors").getElements("option");

    for (var i = 0; i < options.length; i++)
    {
      if (options[i].get("value") == sector)
      {
        alert("Map name must be unique!");
        return;
      }
    }
    Global.Sectors[sector.Name] = sector;
    Global.DisplayedSector = sector;
    Panels.Main.Render();

    Utility.Server.AddSector(sector.Name, sector.Export(), (function(result, error)
    {
      if (error)
        return;

      new Element("option", { value: this.sector.Name, html: this.sector.Name })
        .addEvent("dblclick", function() { Panels.Main.Load() })
        .inject($("SavedSectors"), "bottom");

      $("SavedSectors").selectedIndex = $("SavedSectors").getElements("option").length - 1;

    }).bind({ sector: sector }));
  });
}

Panels.Main.Save = function()
{
  var name = $("SectorName").get("value");
  var code = Utility.Sector.Export();
  var sectors = $("SavedSectors").getElements("option");
  var element = $("SavedSectors").getSelected()[0];

  if (!$chk(element))
    return;

  Global.Sectors[Global.CurrentSector] = Global.DisplayedSector;

  if (name == element.get("value")) // Same name
  {
    Utility.Server.UpdateSector(name, code, $empty); // We don't need to do anything here
  }
  else
  {
    if (sectors.get("value").contains("name"))
    {
      alert("Sector name must be unique!");
      return;
    }

    Utility.Server.RenameSector(element.get("value"), name, (function(result, error)
    {
      if (error)
        return;

      Global.Sectors[this.name] = Global.Sectors[Global.CurrentSector];
      delete Global.Sectors[Global.CurrentSector];
      Global.CurrentSector = this.name;
      this.element.set("value", this.name).set("html", this.name);

      Utility.Server.UpdateSector(this.name, this.code, $empty); // Nothing to be done here

    }).bind({ name: name, element: element, code: code }));
  }
}

Panels.Main.Load = function()
{
  var selected = $("SavedSectors").getSelected()[0];

  if (!$chk(selected))
    return;

  var name = selected.get("value");

  Utility.Server.GetSector(name, (function(result)
  {
    sector = new Sector().Import(result["Code"]);
    Global.DisplayedSector = sector;
    Global.Sectors[this.name] = sector;
    Panels.Main.Render();

  }).bind({ name: name }));
}

Panels.Main.Delete = function()
{
  var selected = $("SavedSectors").getSelected()[0];

  if (!$chk(selected))
    return;

  if (!confirm("Are you sure you want to delete this sector?"))
    return;

  var name = selected.get("value");

  Utility.Server.DeleteSector(name, (function(result, error)
  {
    if (error)
      return;

    this.selected.destroy();
    delete Global.Sectors[Global.CurrentSector];
    Global.CurrentSector = null;
    
  }).bind({ selected: selected }));
}

Panels.Main.Import = function()
{
  var code = prompt("Paste the sector code here:");
  if (!($chk(code) && $chk(code.trim())))
    return;

  var map = JSON.decode(code).map;

  Utility.Server.GetSectorCode(map, (function(result, error)
  {
//    try
//    {
      var sector = new Sector().Import(this.code);
      Global.DisplayedSector = sector;
      Panels.Main.Render();
//    }
//    catch (error)
//    {
//      alert("Error Loading Sector");
//    }
  }).bind({ code: code }));
}

Panels.Main.Export = function()
{
  prompt("Sector Code:", Global.DisplayedSector.Export());
}